package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import com.google.common.base.Objects;
import com.macasa.enumerations.FileSystem;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.IXenVds;
import com.macasa.panel.entity.api.IXenVdsInterface;

@Entity
@Table(name = "xen_vds")
@Getter
@Setter
@ToString(callSuper = true)
public class XenVds extends Server implements IXenVds {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long		serialVersionUID	= -2267863412149693746L;

	@Column(name = "principal_part_size", nullable = false)
	private Integer					principalPartSize;

	@Column(name = "memory_size", nullable = false)
	private Integer					memorySize;

	@Column(name = "swap_size", nullable = true)
	private Integer					swapSize;

	@Enumerated(EnumType.STRING)
	@Column(name = "file_system", nullable = false)
	private FileSystem				fileSystem;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.XenVdsInterface.class)
	@JoinTable(name = "xen_vds_interfaces", joinColumns = @JoinColumn(name = "fk_server_id"), inverseJoinColumns = @JoinColumn(name = "fk_interface_id"))
	private List<IXenVdsInterface>	interfaces;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof XenVds)) {
			return false;
		}

		final XenVds other = CastHelpers.cast(obj, XenVds.class);

		return super.equals(obj) && Objects.equal(this.principalPartSize, other.principalPartSize) && Objects.equal(this.memorySize, other.memorySize)
				&& Objects.equal(this.swapSize, other.swapSize) && Objects.equal(this.fileSystem, other.fileSystem)
				&& Objects.equal(this.interfaces, other.interfaces);
	}

	@Override
	public int hashCode() {
		return super.hashCode() + Objects.hashCode(this.principalPartSize, this.memorySize, this.swapSize, this.fileSystem, this.interfaces);
	}
}
